package com.promise.jokerdream.ui.overlay

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.promise.jokerdream.ui.components.pmBottomSheetDialog
import com.promise.jokerdream.ui.theme.AppFontSizes
import com.promise.jokerdream.ui.theme.AppSpacing

/**
 * 游戏功能半浮层
 */
@Composable
fun GameFunctionBottomSheet(
    onDismiss: () -> Unit,
    onFunctionClick: (GameFunction) -> Unit,
) {
    val compositionContext = rememberCompositionContext()
    
    // 创建 PMBottomSheetDialog
    val pmDialog = pmBottomSheetDialog(
        onDismiss = onDismiss,
        backgroundColor = 0xFFF8F9FA.toInt()
    )
    
    // 设置 Compose 内容
    pmDialog.setContent(compositionContext) {
        MaterialTheme {
            GameFunctionBottomSheetContent(
                onDismiss = {
                    pmDialog.dismiss()
                    onDismiss()
                },
                onFunctionClick = { function ->
                    pmDialog.dismiss()
                    onFunctionClick(function)
                }
            )
        }
    }
    
    // 显示对话框
    LaunchedEffect(Unit) {
        pmDialog.show()
    }

    // 清理资源
    DisposableEffect(Unit) {
        onDispose {
            pmDialog.dismiss()
        }
    }
}

@Composable
private fun GameFunctionBottomSheetContent(
    onDismiss: () -> Unit,
    onFunctionClick: (GameFunction) -> Unit
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = AppSpacing.pageVertical)
            .padding(horizontal = AppSpacing.pageHorizontal)
    ) {
        // 标题栏 - 居中
        Box(
            modifier = Modifier.fillMaxWidth(),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = "乐斗功能",
                fontSize = AppFontSizes.h4,
                fontWeight = FontWeight.SemiBold,
                color = MaterialTheme.colorScheme.onSurface,
                textAlign = TextAlign.Center,
            )
        }
        
        Spacer(modifier = Modifier.height(16.dp))
        
        // 第一张卡片 - 解放双手
        Card(
            modifier = Modifier.fillMaxWidth(),
            shape = RoundedCornerShape(12.dp),
            colors = CardDefaults.cardColors(
                containerColor = Color.White
            ),
            elevation = CardDefaults.cardElevation(defaultElevation = 0.dp)
        ) {
            Column(
                modifier = Modifier.padding(12.dp)
            ) {
                Text(
                    text = "解放双手",
                    fontSize = AppFontSizes.body,
                    fontWeight = FontWeight.SemiBold,
                    color = MaterialTheme.colorScheme.onSurface,
                )
                
                LazyVerticalGrid(
                    columns = GridCells.Fixed(5),
                    horizontalArrangement = Arrangement.spacedBy(0.dp),
                    verticalArrangement = Arrangement.spacedBy(0.dp),
                ) {
                    items(listOf(
                        GameFunction.BAG,            // 背包
                        GameFunction.KNIGHT_DREAM,   // 江湖长梦
                        GameFunction.WULIN_SEARCH,   // 武林查区
                        GameFunction.SNAP_UP,        // 抢购等
                        GameFunction.QUICK_EXCHANGE, // 快速兑换
                        GameFunction.TIMER,          // 定时任务
                        GameFunction.CUSTOM_TASK     // 自定义任务
                    )) { function ->
                        FunctionItem(
                            function = function,
                            onClick = { onFunctionClick(function) }
                        )
                    }
                }
            }
        }

        Spacer(modifier = Modifier.height(16.dp))

        // 第二张卡片 - 每日活动
        Card(
            modifier = Modifier.fillMaxWidth(),
            shape = RoundedCornerShape(12.dp),
            colors = CardDefaults.cardColors(
                containerColor = Color.White
            ),
            elevation = CardDefaults.cardElevation(defaultElevation = 0.dp)
        ) {
            Column(
                modifier = Modifier.padding(12.dp)
            ) {
                Text(
                    text = "每日活动",
                    fontSize = AppFontSizes.body,
                    fontWeight = FontWeight.SemiBold,
                    color = MaterialTheme.colorScheme.onSurface,
                )
                
                LazyVerticalGrid(
                    columns = GridCells.Fixed(5),
                    horizontalArrangement = Arrangement.spacedBy(0.dp),
                    verticalArrangement = Arrangement.spacedBy(0.dp),
                ) {
                    items(listOf(
                        GameFunction.AUTO_PLAY,      // 代玩
                        GameFunction.TOWER_FIGHT,    // 斗神塔
                        GameFunction.ESCORT,         // 劫镖
                        GameFunction.TERRITORY,      // 问鼎天下
                        GameFunction.ROB_TERRITORY,  // 抢地盘
                        GameFunction.FIGHT_FRIEND,   // 乐斗好友
                    )) { function ->
                        FunctionItem(
                            function = function,
                            onClick = { onFunctionClick(function) }
                        )
                    }
                }
            }
        }

        // 垫一个好看一点
        Spacer(modifier = Modifier.height(16.dp))
    }
}

@Composable
private fun FunctionItem(
    function: GameFunction,
    onClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Card(
        modifier = modifier
            .fillMaxWidth()
            .height(80.dp)
            .clickable { onClick() },
        shape = RoundedCornerShape(8.dp),
        colors = CardDefaults.cardColors(
            containerColor = Color.Transparent
        ),
        elevation = CardDefaults.cardElevation(defaultElevation = 0.dp)
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(8.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            // 图标
            Icon(
                painter = painterResource(id = function.iconRes),
                contentDescription = function.displayName,
                modifier = Modifier.size(24.dp),
                tint = MaterialTheme.colorScheme.onSurface
            )
            
            Spacer(modifier = Modifier.height(4.dp))
            
            // 文案
            Text(
                text = function.displayName,
                fontSize = 12.sp,
                fontWeight = FontWeight.Medium,
                color = MaterialTheme.colorScheme.onSurface,
                maxLines = 1,
                textAlign = TextAlign.Center,
                lineHeight = AppFontSizes.mediumBody
            )
        }
    }
}

/**
 * 游戏功能枚举
 */
enum class GameFunction(
    val displayName: String,
    val route: String,
    val iconRes: Int
) {
    AUTO_PLAY(
        displayName = "代玩",
        route = "auto_play",
        iconRes = com.promise.jokerdream.R.drawable.icon_game
    ),
    BAG(
        displayName = "背包",
        route = "bag",
        iconRes = com.promise.jokerdream.R.drawable.icon_package
    ),
    TOWER_FIGHT(
        displayName = "斗神塔",
        route = "tower_fight",
        iconRes = com.promise.jokerdream.R.drawable.icon_tower
    ),
    KNIGHT_DREAM(
        displayName = "江湖长梦",
        route = "knight_dream",
        iconRes = com.promise.jokerdream.R.drawable.icon_knight_dream
    ),
    WULIN_SEARCH(
        displayName = "武林查区",
        route = "wulin_search",
        iconRes = com.promise.jokerdream.R.drawable.icon_wulin
    ),
    ESCORT(
        displayName = "劫镖",
        route = "escort",
        iconRes = com.promise.jokerdream.R.drawable.icon_escort
    ),
    TERRITORY(
        displayName = "问鼎天下",
        route = "territory",
        iconRes = com.promise.jokerdream.R.drawable.icon_territory
    ),
    SNAP_UP(
        displayName = "抢购等",
        route = "snap_up",
        iconRes = com.promise.jokerdream.R.drawable.icon_snap_up
    ),
    TIMER(
        displayName = "定时任务",
        route = "timer",
        iconRes = com.promise.jokerdream.R.drawable.icon_timer
    ),
    QUICK_EXCHANGE(
        displayName = "快速兑换",
        route = "quick_exchange",
        iconRes = com.promise.jokerdream.R.drawable.icon_exchange
    ),
    CUSTOM_TASK(
        displayName = "自定义",
        route = "custom_task",
        iconRes = com.promise.jokerdream.R.drawable.icon_diy
    ),
    ROB_TERRITORY(
        displayName = "抢地盘",
        route = "rob_territory",
        iconRes = com.promise.jokerdream.R.drawable.icon_rob_territory
    ),
    FIGHT_FRIEND(
        displayName = "乐斗好友",
        route = "fight_friend",
        iconRes = com.promise.jokerdream.R.drawable.icon_fight
    )
}
